﻿
KDDataSourceOptions = {
    transport: {
        read: {
            dataType: "json",
            contentType: "application/json",
            type: "POST",
            cache: false
        },
        update: {
            dataType: "json",
            type: "POST",
            cache: false
        },
        destroy: {
            dataType: "json",
            type: "POST",
            cache: false
        },
        create: {
            dataType: "json",
            type: "POST",
            cache: false
        },
        "prefix": ""
    },
    type: "aspnetmvc-ajax",
    "error": KDDataSource_error_handler,
    batch: "false",
    serverPaging: true,
    serverFiltering: true,
    serverSorting: true,
    pageSize: 10,
    page:1,
    schema: {
        data: "Data",
        total: "Total",
        errors: "Errors"
    }
}
KDDSWithRowNOOptions = $.extend(true, {}, KDDataSourceOptions, {
    requestEnd: function (e) {
       
        if(e.response.Errors) return;
        for (var i = 0; i < e.response.Data.length; i++) {
            e.response.Data[i].RowNO = (e.sender.page() - 1) * e.sender.pageSize() + 1 + i;
        }
    }
});


function KDDataSource_error_handler(e) {
    if (e.errors)
        if (e.errors == "授权失败") {
            ReLogin();
        }
        else {
            //alert("出现异常，建议你刷新本页面" + e.errors);
        }
    else {
           //alert("出现异常，建议你刷新本页面" + e.errorThrown);
    }
    //alert(e.errorThrown);
}

KDGridColumnfilterable = {
    "messages": {
        "info": "显示符合以下条件的行",
        "isTrue": "为真",
        "isFalse": "为假",
        "filter": "过滤",
        "clear": "清除过滤",
        "and": "并且",
        "or": "或者",
        "selectValue": "-选择值-",
        "operator": "操作",
        "value": "值",
        "cancel": "取消"
    },
    "operators": {
        "string": { "eq": "等于", "neq": "不等于", "startswith": "开始于", "endswith": "结束于", "contains": "包含", "doesnotcontain": "不包含" },
        "number": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" },
        "date": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" },
        "enums": { "eq": "等于", "neq": "不等于" }
    }
}
KDGridpageable = {
    numeric: true,
    input: true,
    refresh: true,
    "buttonCount": 10,
    "messages": {
        "display": "显示条目 {0} - {1} 共 {2}",
        "empty": "没有可显示的记录。",
        "page": "页",
        "of": "共 {0}",
        "first": "首页",
        "previous": "前页",
        "next": "后页",
        "last": "末页",
        "refresh": "刷新",
        "morePages": "更多页"
    }
}
KDGridpageableSum = {
    numeric: false,
    input: false,
    refresh: false,
    previousNext: false,
    "buttonCount": 10,
    "messages": {
        "display": "记录数 {2}",
        "empty": "没有可显示的记录。",
        "page": "页",
        "of": "共 {0}",
        "first": "首页",
        "previous": "前页",
        "next": "后页",
        "last": "末页",
        "refresh": "刷新",
        "morePages": "更多页"
    }
}
KDGridpageableTotal = {
    numeric: false,
    input: false,
    refresh: false,
    previousNext: false,
    "buttonCount": 10,
    "messages": {
        "display": "总数 {2}",
        "empty": "没有可显示的记录。",
        "page": "页",
        "of": "共 {0}",
        "first": "首页",
        "previous": "前页",
        "next": "后页",
        "last": "末页",
        "refresh": "刷新",
        "morePages": "更多页"
    }
}
KDGrideditable = {
    "confirmation": "确定要删除吗？",
    "confirmDelete": "删除",
    "cancelDelete": "取消",
    "mode": "incell",
    "template": null,
    "create": true,
    "update": true,
    "destroy": true
}

KDGridmessages = {
    commands: {
        cancel: "取消",
        canceledit: "撤销修改",
        create: "新增",
        destroy: "删除",
        edit: "修改",
        save: "提交修改",
        select: "选择",
        update: "更新"
    },
    noRecords:"暂无数据"
}






kendoValidatorOptions = {
    messages: {
        required: "必须录入",
        email: "邮件格式错误",
        pattern: "格式错误",
        minlength: "必须录入足够长度",
        isdate: "日期格式错误",
        isdatetime: "日期时间格式错误"
    }
    , rules: {
        minlength: function (input) {
            if (input.is("[minlength]") && input.val() != "") {

                var minlen = parseInt(input.attr("minlength"));
                return input.val().length >= minlen;
            }
            return true;
        },
        compare: function (input) {
            if (input.is("[compare]") && input.val() != "") {
                return $("#" + input.attr("compare")).val() == input.val();
            }
            return true;
        },
        remote: function (input) {

            if (input.is("[remote]") && input.val() != "") {
                var result = false;
                kendo.ui.progress(input.parent(), true);

                $.ajax({
                    url: input.attr("remote"),
                    data: { id: input.val() },
                    async: false,
                    method: "post",
                    dataType: 'json',
                    success: function (r) {
                        if (r.code == "") {
                            result = true;
                        }
                    }
                });
                kendo.ui.progress(input.parent(), false);
                return result;
            }
            return true;
        },
        isdate: function (input) {
            var kdp = input.data("kendoDatePicker");
            if (kdp == null) return true;
            if (input.val() == "") return true;
            if (kendo.parseDate(input.val(), "yyyy-MM-dd") == null) {
                return false;
            }
            else {
                return true;
            }
        },
        isdatetime: function (input) {
            var kdp = input.data("kendoDateTimePicker");
            if (kdp == null) return true;
            if (input.val() == "") return true;
            if (kendo.parseDate(input.val(), "yyyy-MM-dd HH:mm") == null) {
                return false;
            }
            else {
                return true;
            }
        }

    }
}
//去掉国际日期转换，直接使用当地日期
Date.prototype.toISOString = function () {
    return kendo.toString(this, "yyyy-MM-ddTHH:mm:ss");
};

Date.prototype.toString = function (formatStr) {
    if (!formatStr) {
        formatStr = "yyyy-MM-dd HH:mm:ss";
    }
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    str = str.replace(/MM/, this.getMonth() >= 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
    str = str.replace(/M/g, this.getMonth());
    str = str.replace(/w|W/g, Week[this.getDay()]);
    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());
    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());
    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());
    return str;
}

var isArray = function (obj) {
    return obj && !(obj.propertyIsEnumerable('length')) && typeof obj === 'object' && typeof obj.length === 'number';
};


function KendoCombileFilter(operator, f1, f2) {
    if (!f1) return f2;
    if (!f2) return f1;
    var filter = {
        logic: operator,
        filters: []
    }
    filter.filters = filter.filters.concat(f1, f2);
    return filter;
}